package app.util.workers;

import javax.swing.SwingWorker;

import app.model.IMDBModel;
import app.util.BigFile;
import app.util.DHTConnector;

public class LoadDataWorker<T, V> extends SwingWorker<T, V>{
	private IMDBModel context;
	private String fileName;
	public LoadDataWorker(IMDBModel context, String fileName){
		this.context = context;
		this.fileName = fileName;
	}

	@Override
	protected T doInBackground() throws Exception {
		try {
			BigFile file = new BigFile(fileName);
			for(String line : file){
				//get all words in the line and use them as a key for the full line
				String keys[] = line.split(" ");
				for(int i = 0; i < keys.length; i++){
					//remove " from key
					keys[i] = keys[i].replaceAll("[\"(){}]", "");
					//store the key value pair in the dht
					if(!keys[i].equalsIgnoreCase(""))DHTConnector.insert(keys[i], line);
				}
			}
			context.loadDataFinished();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
}